USE SAS
GO

 IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'InsUpdUserDetails')
	BEGIN
		DROP  Procedure  dbo.InsUpdUserDetails
	END
GO

CREATE Procedure dbo.InsUpdUserDetails (
	@USERNO					VARCHAR(100)
,	@USERGUID				VARCHAR(100)
,	@FIRSTNAME				VARCHAR(100)
,	@LASTNAME				VARCHAR(100)
,	@LOGINID				VARCHAR(100) = ''
,	@PASSWORD				VARCHAR(100) = ''
,	@ISACTIVE				VARCHAR(10)	 = 'X'
,	@CANCREATEPARENTACCT	VARCHAR(10)	 = 'X'
,	@CANEDITPRODUCTMASTER	VARCHAR(10)	 = 'X'
,	@ISSUPERUSER			VARCHAR(10)	 = 'X'
,	@USERID					VARCHAR(10)	 = '0'
-- --------------------------------------------
,	@PARTNERDIVISIONID		VARCHAR(10)	 = '1'
)
AS
BEGIN

	-- ************************************************
	-- BELOW PARAMETERS ARE CURRENTLY HARDCODED
	-- START: DO NOT MODIFY THE BELOW CODE
	-- ************************************************

	DECLARE
		@MANAGERID				VARCHAR(10)

	SELECT
		@MANAGERID				= NULL

	-- ************************************************
	-- END: DO NOT MODIFY THE BELOW CODE
	-- ************************************************

	IF (CAST(@USERNO AS INT) = 0) AND EXISTS(SELECT 1 FROM TR_USER WHERE LOGINID = @LOGINID)
	BEGIN
		SELECT 
			'<DATA>
				<RESPONSE>
					<RESULT>E</RESULT>
					<MESSAGE><![CDATA[Select a different Login ID]]></MESSAGE>
				</RESPONSE>
			</DATA>' AS Result
		RETURN
	END


	DECLARE @C_PARTNERCATEGORY	INT
	DECLARE @C_PARTNERTYPE		INT
	
	SET @C_PARTNERCATEGORY	= 1
	SET @C_PARTNERTYPE		= 4

	SET @ISACTIVE = CASE WHEN @ISACTIVE IN ('X', '1') THEN '1' ELSE '0' END
	SET @CANCREATEPARENTACCT = CASE WHEN @CANCREATEPARENTACCT IN ('X','1') THEN '1' ELSE '0' END
	SET @CANEDITPRODUCTMASTER = CASE WHEN @CANEDITPRODUCTMASTER IN ('X','1') THEN '1' ELSE '0' END
	SET @ISSUPERUSER = CASE WHEN @ISSUPERUSER IN ('X','1') THEN '1' ELSE '0' END

	BEGIN TRAN
		BEGIN TRY
			IF EXISTS(SELECT 1 FROM TR_PARTNER WHERE PartnerGUID = @USERGUID) 
			BEGIN		-- UPDATE
				UPDATE TR_PARTNER
				SET	PartnerFirstName	= @FIRSTNAME
				,	PartnerLastName		= @LASTNAME
				,	PartnerDivision		= @PARTNERDIVISIONID
				,	isActive			= @ISACTIVE
				,	ChangedBy			= @USERID
				,	ChangeDate			= GETDATE()
				WHERE
					PartnerNo			= @USERNO
				AND	PartnerGuid			= @USERGUID
				AND	PartnerCategory		= @C_PARTNERCATEGORY
				AND	PartnerType			= @C_PARTNERTYPE
			
			END
			ELSE
			BEGIN		-- INSERT
				SELECT @USERNO	= ISNULL(MAX(PartnerNo), 0) +1 FROM TR_PARTNER

				INSERT INTO TR_PARTNER (PartnerNo, PartnerGUID, PartnerCategory, PartnerType, 
										PartnerFirstName, PartnerLastName,  
										PartnerDivision, isActive, 
										CreatedBy, CreateDate, ChangedBy, ChangeDate)
				VALUES(
					@USERNO					-- PartnerNo
				,	@USERGUID				-- PartnerGUID
				,	@C_PARTNERCATEGORY		-- PartnerCategory
				,	@C_PARTNERTYPE			-- PartnerType
				,	@FIRSTNAME				-- FirstName
				,	@LASTNAME				-- LastName
				,	@PARTNERDIVISIONID		-- PartnerDivision
				,	@ISACTIVE				-- isActive
				,	@USERID					-- CreatedBy
				,	GetDate()				-- CreateDate
				,	@USERID					-- ChangedBy
				,	GetDate()				-- ChangeDate
				)
			END

		
			IF EXISTS( SELECT 1 FROM TR_USER WHERE CAST(PartnerGUID AS VARCHAR(100)) = @USERGUID)
			BEGIN
				UPDATE TR_USER
				SET	PartnerNo			= @USERNO
				,	LoginId				= @LOGINID
				,	[Password]			= @PASSWORD
				,	CanCreateParentAcct	= @CANCREATEPARENTACCT
				,	CanEditProductMaster= @CANEDITPRODUCTMASTER
				,	ManagerId			= @MANAGERID
				,	isSuperUser			= @ISSUPERUSER
				,	isActive			= @ISACTIVE
				,	PartnerGuid			= @USERGUID
				FROM TR_USER
				WHERE 
					CAST(PartnerGUID AS VARCHAR(100)) = @USERGUID
			END
			ELSE
			BEGIN
				INSERT INTO TR_USER (PartnerNo, LoginId, [Password], CanCreateParentAcct, CanEditProductMaster, 
									ManagerId, isSuperUser, isActive, PartnerGuid)
				VALUES(
					@USERNO					--	PartnerNo
				,	@LOGINID				--	LoginId
				,	@PASSWORD				--	Password
				,	@CANCREATEPARENTACCT	--	CanCreateParentAcct
				,	@CANEDITPRODUCTMASTER	--	CanEditProductMaster
				,	@MANAGERID				--	ManagerId
				,	@ISSUPERUSER			--	isSuperUser
				,	@ISACTIVE				--	isActive
				,	@USERGUID				--	PartnerGuid
				)

			END

			COMMIT TRAN

			SELECT 
				'<DATA>
					<RESPONSE>
						<RESULT>S</RESULT>
						<MESSAGE>User details saved successfully</MESSAGE>
					</RESPONSE>
				</DATA>' AS Result
		END TRY
		BEGIN CATCH
			ROLLBACK TRAN
			SELECT 
				'<DATA>
					<RESPONSE>
						<RESULT>E</RESULT>
						<MESSAGE><![CDATA[Unable to save User details. '+ERROR_MESSAGE()+']]></MESSAGE>
					</RESPONSE>
				</DATA>' AS Result
		END CATCH
END
GO
